import java.util.*;
public class GetValidT9Words {
  public List<String> getValidT9Words(String num, String[] words) {
      List<String> result = new ArrayList<>();
      for (String w : words) {
        if (isValidT9Word(num, w)) {
          result.add(w);
        }
      }
      return result;
  }
  private boolean isValidT9Word(String num, String word) {
    for (int i = 0; i < num.length(); i++) {
      char c = num.charAt(i);
      if (c == '0') {
        if (word.charAt(i)!= 'z') {
          return false;
        }
      } else {
        if (c != "22233344455566677778889999".charAt(word.charAt(i) - 'a')) {
          return false;
        }
      }
    }
    return true;
  }

  public static void main(String[] args) {
    GetValidT9Words g = new GetValidT9Words();
    String[] words = {"tree", "used", "zoo"};
    String num = "8733";
    System.out.println(g.getValidT9Words(num, words));
  }
}
